package cn.nfwx.tv.helper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import java.util.ArrayList;
import java.util.List;

/**
 * Home键的监听按钮
 * Created by zhouzechao on 2017/12/4.
 */
public class HomeWatcherHelper {
    private static HomeWatcherHelper instance;
    private HomeWatcherReceiver mHomeKeyReceiver;

    public static HomeWatcherHelper getInstance() {
        if (instance == null) {
            synchronized (instance) {
                if (instance == null) {
                    instance = new HomeWatcherHelper();
                }
            }
        }
        return instance;
    }

    public void registerHomeKeyReceiver(Context context, HomeWatcherListener homeWatcherListener) {
        if (mHomeKeyReceiver == null) {
            mHomeKeyReceiver = new HomeWatcherReceiver();
        }
        IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        mHomeKeyReceiver.addHomeWatcherListener(homeWatcherListener);
        context.registerReceiver(mHomeKeyReceiver, homeFilter);
    }

    public void unregisterHomeKeyReceiver(Context context, HomeWatcherListener homeWatcherListener) {
        if (mHomeKeyReceiver != null) {
            mHomeKeyReceiver.removeHomeWatcherListener(homeWatcherListener);
            context.unregisterReceiver(mHomeKeyReceiver);
            if (mHomeKeyReceiver.isClear()) {
                mHomeKeyReceiver = null;
            }
        }
    }

    public interface HomeWatcherListener {
        void onHomeKey();
    }

    static class HomeWatcherReceiver extends BroadcastReceiver {

        private static final String LOG_TAG = "HomeReceiver";
        private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
        private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
        private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";

        private List<HomeWatcherListener> homeWatcherListeners = new ArrayList<>();

        public void removeHomeWatcherListener(HomeWatcherListener homeWatcherListener) {
            if (homeWatcherListener != null) {
                homeWatcherListeners.remove(homeWatcherListener);
            }
        }

        public void addHomeWatcherListener(HomeWatcherListener homeWatcherListener) {
            if (homeWatcherListener != null) {
                homeWatcherListeners.add(homeWatcherListener);
            }
        }

        public boolean isClear() {
            return homeWatcherListeners.isEmpty();
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                // android.intent.action.CLOSE_SYSTEM_DIALOGS
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                boolean homeclickstatus = false;
                if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
                    // 短按Home键
                    homeclickstatus = true;
                } else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
                    // 长按Home键 或者 activity切换键
                    homeclickstatus = true;
                } else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
                    // 锁屏
                    homeclickstatus = true;
                } else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
                    // samsung 长按Home键
                    homeclickstatus = true;
                }
                if (homeclickstatus) {
                    for (HomeWatcherListener homeWatcherListener : homeWatcherListeners) {
                        homeWatcherListener.onHomeKey();
                    }
                }
            }
        }
    }
}
